<?php
/*
 * Author :pandiam
 */
session_start();

if (isset($_SESSION['uid']) && $_SESSION['uid'] != -1)
{
    header('Location: index.php');
}

require_once ('../bl/bl.php');
require_once ('include/recaptchalib.php');
require_once ('../Frameworks/Objects/user.php');
require_once ('Include/functions.php');

$privatekey = "6Lcc58QSAAAAAOan7XRHu6tn-B9JhWwtq3vhzcd5";

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $resp = recaptcha_check_answer($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"],
        $_POST["recaptcha_response_field"]);

    if (!$resp->is_valid)
        echo ("Error - Captcha wrong");

    if ($_POST['password'] != $_POST['password2'])
        echo ("Error - Passwords don't match. Please go back and try again.");

    if ($_POST['email'] != $_POST['email2'])
        echo ("Error - Emails don't match.");

    $user = new User();
    $user->SetUsername($_POST['username']);
    $user->SetPassword($_POST['password']);
    $user->SetEmail($_POST['email']);
    $user->SetName($_POST['name']);
    $user->SetSurname($_POST['surname']);
    $res = BL_registerUser($user);

    if ($res >= 0)
    {
//        $_SESSION['uid'] = $res;
//        $_SESSION['usename'] = $_POST['username'];
        UI_registerError('msg_success', "Registration successful!");
        UI_registerError('msg_info', "Confirmation mail has been sent to the registered email address.");
        UI_registerError('msg_info', "DON'T forget to activate your account first!!");
        header("location: login.php");
        exit(0);
    }
    else
    {
        BL_RegisterError($res);
        BL_FlushErrors();
        header("location: register.php");
        exit(0);
    }
}
?>
